<html>
<head><meta charset="utf-8"><title>Transmute with unchecked layouts · t-compiler · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/index.html">t-compiler</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Transmute.20with.20unchecked.20layouts.html">Transmute with unchecked layouts</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="212139422"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Transmute%20with%20unchecked%20layouts/near/212139422" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mikail Bagishov <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Transmute.20with.20unchecked.20layouts.html#212139422">(Oct 02 2020 at 23:13)</a>:</h4>
<p>Currently <code>transmute</code> is too conservative with it layout checks. It complicates unsafe generic code as one now has to use <code>transmute_copy</code> etc.<br>
Could it be possible to add (probably perma-unstable) <code>core::intrinsics::transmute_unchecked</code> thing that does not validate size? IMHO some libstd code will become simpler.</p>



<a name="212139718"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Transmute%20with%20unchecked%20layouts/near/212139718" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Transmute.20with.20unchecked.20layouts.html#212139718">(Oct 02 2020 at 23:16)</a>:</h4>
<p>isn't that the same as <code>transmute_copy</code>?</p>



<a name="212140542"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Transmute%20with%20unchecked%20layouts/near/212140542" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Transmute.20with.20unchecked.20layouts.html#212140542">(Oct 02 2020 at 23:29)</a>:</h4>
<p>I guess that's a <em>consuming</em> <code>transmute_copy</code> -- <code>transmute_move</code>?</p>



<a name="212141268"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Transmute%20with%20unchecked%20layouts/near/212141268" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Transmute.20with.20unchecked.20layouts.html#212141268">(Oct 02 2020 at 23:43)</a>:</h4>
<p>Given that the safe transmute project is currently "in progress", this feels less likely to be added. Might as well just wait a little longer and use safe transmutes.</p>



<a name="212155701"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Transmute%20with%20unchecked%20layouts/near/212155701" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Transmute.20with.20unchecked.20layouts.html#212155701">(Oct 03 2020 at 06:14)</a>:</h4>
<p>With const wf bounds we can make it possible to check that the size of the generic parameters of transmute are the same and thus allow people to bubble up this requirement in their generic bounds</p>



<a name="212158163"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Transmute%20with%20unchecked%20layouts/near/212158163" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Transmute.20with.20unchecked.20layouts.html#212158163">(Oct 03 2020 at 07:22)</a>:</h4>
<p>So this is how a generic transmute can be implemented using const wf bounds</p>
<p><a href="https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=9c623c26f215e8c99a5688b287368b67">https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=9c623c26f215e8c99a5688b287368b67</a></p>



<a name="212158177"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Transmute%20with%20unchecked%20layouts/near/212158177" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Transmute.20with.20unchecked.20layouts.html#212158177">(Oct 03 2020 at 07:23)</a>:</h4>
<p>this still validates sizes but does so in a generic way</p>



<a name="212158199"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Transmute%20with%20unchecked%20layouts/near/212158199" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Transmute.20with.20unchecked.20layouts.html#212158199">(Oct 03 2020 at 07:23)</a>:</h4>
<p>the only issue is that I don't think we should add <code>const_evaluatable_checked</code> to std yet</p>



<a name="212158203"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Transmute%20with%20unchecked%20layouts/near/212158203" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Transmute.20with.20unchecked.20layouts.html#212158203">(Oct 03 2020 at 07:24)</a>:</h4>
<p>even if it is not yet used in any userfacing api</p>



<a name="212158245"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Transmute%20with%20unchecked%20layouts/near/212158245" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Transmute.20with.20unchecked.20layouts.html#212158245">(Oct 03 2020 at 07:24)</a>:</h4>
<p>as it might still be broken in subtle ways</p>



<a name="212190328"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Transmute%20with%20unchecked%20layouts/near/212190328" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Transmute.20with.20unchecked.20layouts.html#212190328">(Oct 03 2020 at 21:17)</a>:</h4>
<p>And in the meantime:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">unsafe</span><span class="w"></span>
<span class="k">fn</span> <span class="nf">transmute_unchecked</span><span class="o">&lt;</span><span class="n">T</span><span class="p">,</span><span class="w"> </span><span class="n">U</span><span class="o">&gt;</span><span class="w"> </span><span class="p">(</span><span class="n">it</span>: <span class="nc">T</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nc">U</span><span class="w"></span>
<span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">transmute_copy</span>::<span class="o">&lt;</span><span class="n">T</span><span class="p">,</span><span class="w"> </span><span class="n">U</span><span class="o">&gt;</span><span class="p">(</span><span class="w"> </span><span class="o">&amp;*</span><span class="n">ManuallyDrop</span>::<span class="n">new</span><span class="p">(</span><span class="n">it</span><span class="p">)</span><span class="w"> </span><span class="p">)</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="212218179"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Transmute%20with%20unchecked%20layouts/near/212218179" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Transmute.20with.20unchecked.20layouts.html#212218179">(Oct 04 2020 at 11:03)</a>:</h4>
<p><span class="user-mention silent" data-user-id="216206">lcnr</span> <a href="#narrow/stream/131828-t-compiler/topic/Transmute.20with.20unchecked.20layouts/near/212158163">said</a>:</p>
<blockquote>
<p>So this is how a generic transmute can be implemented using const wf bounds</p>
<p><a href="https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=9c623c26f215e8c99a5688b287368b67">https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=9c623c26f215e8c99a5688b287368b67</a></p>
</blockquote>
<p>does this mean we can change <code>mem::transmute</code> from a reexport to a proper wrapper function and make the actual intrinsic private and unstable? :D<br>
(though likely at this point too much code directly uses <code>intrinsics::transmute</code> and we cannot take this back any more...)</p>



<a name="212218474"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Transmute%20with%20unchecked%20layouts/near/212218474" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Transmute.20with.20unchecked.20layouts.html#212218474">(Oct 04 2020 at 11:12)</a>:</h4>
<p>We could remove the intrinsic and make the intrinsic be the reexport. There's precedent for this iirc</p>



<a name="212219377"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Transmute%20with%20unchecked%20layouts/near/212219377" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Transmute.20with.20unchecked.20layouts.html#212219377">(Oct 04 2020 at 11:39)</a>:</h4>
<p>what now, remove it or make it a reexport?^^</p>



<a name="212219461"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Transmute%20with%20unchecked%20layouts/near/212219461" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Transmute.20with.20unchecked.20layouts.html#212219461">(Oct 04 2020 at 11:41)</a>:</h4>
<p>the intrinsic is stabilized</p>



<a name="212219465"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Transmute%20with%20unchecked%20layouts/near/212219465" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Transmute.20with.20unchecked.20layouts.html#212219465">(Oct 04 2020 at 11:41)</a>:</h4>
<p>so we have to have at least a reexport here</p>



<a name="212219509"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Transmute%20with%20unchecked%20layouts/near/212219509" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Transmute.20with.20unchecked.20layouts.html#212219509">(Oct 04 2020 at 11:42)</a>:</h4>
<p>which we can then deprecate hopefully</p>



<a name="212219511"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Transmute%20with%20unchecked%20layouts/near/212219511" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Transmute.20with.20unchecked.20layouts.html#212219511">(Oct 04 2020 at 11:42)</a>:</h4>
<p>but we don't need transmute to be an intrinsic afterwards</p>



<a name="212219515"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Transmute%20with%20unchecked%20layouts/near/212219515" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Transmute.20with.20unchecked.20layouts.html#212219515">(Oct 04 2020 at 11:42)</a>:</h4>
<p>(if it doesn't hurt perf)</p>



<a name="212223326"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Transmute%20with%20unchecked%20layouts/near/212223326" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Transmute.20with.20unchecked.20layouts.html#212223326">(Oct 04 2020 at 13:27)</a>:</h4>
<p><span class="user-mention silent" data-user-id="216206">lcnr</span> <a href="#narrow/stream/131828-t-compiler/topic/Transmute.20with.20unchecked.20layouts/near/212219461">said</a>:</p>
<blockquote>
<p>the intrinsic is stabilized</p>
</blockquote>
<p>it is intentionally stabilized at <code>mem::transmute</code> and accidentally stabilized at <code>intrinsics::transmute</code>. I cannot tell which of the two reexports of the same item you are referring to.</p>



<a name="212223339"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Transmute%20with%20unchecked%20layouts/near/212223339" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Transmute.20with.20unchecked.20layouts.html#212223339">(Oct 04 2020 at 13:27)</a>:</h4>
<p>I wasnt talking about making it not an intrinsic, I was talking about not re-exporting the underlying intrinsic. We don't usually re-export intrinsics, but we had to for <code>transmute</code> because of the size check.</p>



<a name="212223382"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Transmute%20with%20unchecked%20layouts/near/212223382" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Transmute.20with.20unchecked.20layouts.html#212223382">(Oct 04 2020 at 13:28)</a>:</h4>
<p>It is not possible to make a generic wrapper around <code>transmute</code>. The input and output types of <code>transmute</code> must be known before monomorphization.</p>



<a name="212223402"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Transmute%20with%20unchecked%20layouts/near/212223402" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Transmute.20with.20unchecked.20layouts.html#212223402">(Oct 04 2020 at 13:29)</a>:</h4>
<p>that's what I said</p>



<a name="212223447"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Transmute%20with%20unchecked%20layouts/near/212223447" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Transmute.20with.20unchecked.20layouts.html#212223447">(Oct 04 2020 at 13:30)</a>:</h4>
<p>but this thread describes a way that seems to make it possible, hence my reaction :D</p>



<a name="212223548"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Transmute%20with%20unchecked%20layouts/near/212223548" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Transmute.20with.20unchecked.20layouts.html#212223548">(Oct 04 2020 at 13:33)</a>:</h4>
<p>Yes, we will be able to wrap transmute. But in fact we will be able to remove the intrinsic entirely and just put a reexport of the library implementation in its place</p>



<a name="212223602"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Transmute%20with%20unchecked%20layouts/near/212223602" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Transmute.20with.20unchecked.20layouts.html#212223602">(Oct 04 2020 at 13:34)</a>:</h4>
<p>Ah ok. Just found the playground link. I did prefer using a <code>transmute</code> intrinsic inside the wrapper over using <code>MaybeUninit</code>. The later requires much more work from the optimizer to make it fast.</p>



<a name="212224396"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Transmute%20with%20unchecked%20layouts/near/212224396" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Transmute.20with.20unchecked.20layouts.html#212224396">(Oct 04 2020 at 13:56)</a>:</h4>
<p>yeah, we can use a <code>transmute_unchecked_size</code> intrinsic or put the const evaluatable bound on the intrinsic itself</p>



<a name="212224431"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Transmute%20with%20unchecked%20layouts/near/212224431" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Transmute.20with.20unchecked.20layouts.html#212224431">(Oct 04 2020 at 13:57)</a>:</h4>
<p>if perf ends up being an issue here</p>



<a name="212224438"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Transmute%20with%20unchecked%20layouts/near/212224438" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Transmute.20with.20unchecked.20layouts.html#212224438">(Oct 04 2020 at 13:57)</a>:</h4>
<p>will take a while until we will have to worry about this though</p>



<a name="212224576"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Transmute%20with%20unchecked%20layouts/near/212224576" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Transmute.20with.20unchecked.20layouts.html#212224576">(Oct 04 2020 at 14:01)</a>:</h4>
<p>The MCP for a custom transmute rvalue has just finished FCP, so I'm not worried about perf here as we can just replace the lib impl with an rvalue where appropriate</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>